package com.bjsxt.base.sync001;

import java.util.concurrent.*;

public class RejectedExecutionTest {

    private static class Worker implements Runnable {
        public void run() {
            System.out.println(Thread.currentThread().getName() + " is running");
        }
    }

    public static void main(String[] args) {
        int corePoolSize = 5;
        int maxPoolSize = 10;
        long keepAliveTime = 5;
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(10);
        //拒绝策略1：将抛出 RejectedExecutionException.
        RejectedExecutionHandler handler =
                new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor executor = new ThreadPoolExecutor
                (corePoolSize, maxPoolSize,
                        keepAliveTime, TimeUnit.SECONDS,
                        queue, handler);
        for (int i = 0; i < 100; i++) {
            executor.execute(new Worker());
        }
        executor.shutdown();
    }
}
